package com.hkbigdata.datagovernance.governance.assess.quality;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hkbigdata.datagovernance.ds.bean.TDsTaskInstance;
import com.hkbigdata.datagovernance.ds.service.TDsTaskInstanceService;
import com.hkbigdata.datagovernance.governance.assess.Assessor;
import com.hkbigdata.datagovernance.governance.bean.AssessParam;
import com.hkbigdata.datagovernance.governance.bean.GovernanceAssessDetail;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;
//表产出时效监控表产出时效监控

//前一天产出时效，超过前{days}天产出时效平均值n%
//则给0分，其余10分
@Component("TIME_LINESS")
public class TimelinessAssessor extends Assessor {

    @Autowired
    TDsTaskInstanceService tDsTaskInstanceService;

    @Override
    public void assess(GovernanceAssessDetail governanceAssessDetail, AssessParam assessParam) throws Exception {
        String assessDate = assessParam.getAssessDate();
        String metricParamsJson = assessParam.getGovernanceMetric().getMetricParamsJson();
        JSONObject paramsJsonObj = JSON.parseObject(metricParamsJson);
        Integer days = paramsJsonObj.getInteger("days");
        BigDecimal percent = paramsJsonObj.getBigDecimal("percent");

        //取得今天最后一次成功的任务实例的计算时长
        QueryWrapper<TDsTaskInstance> queryWrapper = new QueryWrapper<TDsTaskInstance>().eq("date_format(start_time,'%Y-%m-%d')", assessDate).eq("state","7").orderByDesc("start_time").select(" ( end_time-start_time ) as  duration"); //.last("limit 1");
        Map<String, Object> durRsMap = tDsTaskInstanceService.getMap(queryWrapper);
        Long curDuration=0L;
        if(durRsMap!=null){
            curDuration= (Long)  durRsMap.get("duration");
        }


        //取得前n天的所有成功的任务实例取得平均值
        Date curDate = DateUtils.parseDate(assessDate, new String[]{"yyyy-MM-dd"});
        Date endDate = DateUtils.addDays(curDate, -1);
        Date fromDate = DateUtils.addDays(curDate, 0-days);


        QueryWrapper<TDsTaskInstance> avgQueryWrapper = new QueryWrapper<TDsTaskInstance>().eq("state","7").between( "start_time" , fromDate, endDate) .select("avg(end_time-start_time) avg_duration");
        Map<String, Object> avgRsMap = tDsTaskInstanceService.getMap(avgQueryWrapper);
        BigDecimal avgCurDuration=BigDecimal.ONE ;
        if(avgRsMap!=null){
            avgCurDuration= (BigDecimal)  avgRsMap.get("avg_duration");
        }
        governanceAssessDetail.setAssessComment(" 当日任务耗时"+curDuration+"秒,前"+days+"天平均值为"+avgCurDuration +"秒 ");
        // 平均值乘以百分比 比较当此时长
        if(BigDecimal.valueOf(curDuration).compareTo(   avgCurDuration.multiply(percent).movePointRight(2)) >0){
            governanceAssessDetail.setAssessScore(BigDecimal.ZERO);
            governanceAssessDetail.setAssessProblem("时长低于前"+days+"天总平均时长的"+percent+"%");
        }

    }
}

